package com.wzt.lianfirecontrol.adapter.xuncha;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaPlanModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.ChildTouchListener;

public class XunChaListAdapter extends BaseRecyclerAdapter<XunChaPlanModel> {

    private Context context;

    private boolean isHistory = false;

    public boolean isHistory() {
        return isHistory;
    }

    public void setHistory(boolean history) {
        isHistory = history;
    }

    public XunChaListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xuncha_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaPlanModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_title;
        private TextView tv_day_times;
        private TextView tv_status;
        private RecyclerView rlv_build_list;
        private XunChaBuildAdapter xunChaBuildAdapter;
        private LinearLayout ll_date;
        private TextView tv_date;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_day_times = itemView.findViewById(R.id.tv_day_times);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.rlv_build_list = itemView.findViewById(R.id.rlv_build_list);
        myViewHolder.ll_date = itemView.findViewById(R.id.ll_date);
        myViewHolder.tv_date = itemView.findViewById(R.id.tv_date);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myViewHolder.rlv_build_list.setLayoutManager(linearLayoutManager);
        myViewHolder.xunChaBuildAdapter = new XunChaBuildAdapter(context);
        myViewHolder.xunChaBuildAdapter.setHistory(isHistory);
        myViewHolder.rlv_build_list.setAdapter(myViewHolder.xunChaBuildAdapter);
        ChildTouchListener childTouchListener = new ChildTouchListener(itemView);
        myViewHolder.xunChaBuildAdapter.setOnTouchListener(childTouchListener);
        myViewHolder.xunChaBuildAdapter.setHistory(isHistory);
        myViewHolder.rlv_build_list.setOnTouchListener(childTouchListener);
    }

    private void setItemData(MyViewHolder viewHolder, XunChaPlanModel data, int realPosition) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (realPosition == 0){
            if (isHistory){
                layoutParams.setMargins(0, Utils.dip2px(context, 0.5f), 0 ,Utils.dip2px(context, 0.5f));
            }else {
                layoutParams.setMargins(0, Utils.dip2px(context, 15), 0 ,Utils.dip2px(context, 15));
            }

        }else {
            if (isHistory){
                layoutParams.setMargins(0, 0, 0 ,Utils.dip2px(context, 0.5f));
            }else {
                layoutParams.setMargins(0, 0, 0 ,Utils.dip2px(context, 15));
            }
        }
        if (isHistory){
            viewHolder.ll_item_content.setBackgroundColor(context.getResources().getColor(R.color.divider_line_color));
        }
        viewHolder.tv_title.setText(data.getPlanName());
        viewHolder.tv_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(data.getPlanName()) && 0 < viewHolder.tv_title.getLayout().getEllipsisCount(viewHolder.tv_title.getLineCount() - 1)){
                    ToastUtils.showToast(context, data.getPlanName());
                }
            }
        });
        if (StringUtils.isEmpty(data.getCycle()) || StringUtils.isEmpty(data.getCycleUnit())){
            viewHolder.tv_day_times.setVisibility(View.INVISIBLE);
        }else {
            viewHolder.tv_day_times.setVisibility(View.VISIBLE);
            viewHolder.tv_day_times.setText(data.getCycle() + data.getCycleUnit());
        }

        if (StringUtils.isEmpty(data.getStatus())){
            viewHolder.tv_status.setVisibility(View.GONE);
        }else {
            viewHolder.tv_status.setVisibility(View.VISIBLE);
            GradientDrawable gradientDrawable = new GradientDrawable();
            if ("0".equals(data.getStatus())){
                viewHolder.tv_status.setText("待检查");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
            }else if ("1".equals(data.getStatus())){
                viewHolder.tv_status.setText("巡查中");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
            }else if ("2".equals(data.getStatus())) {
                viewHolder.tv_status.setText("已完成");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
            }else {
                viewHolder.tv_status.setText("已过期");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.hint_text_color));
            }
            gradientDrawable.setCornerRadius(Utils.dip2px(context, 10));
            viewHolder.tv_status.setBackground(gradientDrawable);
        }

        if (null != data.getBuildingList() && data.getBuildingList().size() > 0){
            viewHolder.rlv_build_list.setVisibility(View.VISIBLE);
            viewHolder.xunChaBuildAdapter.clear();
            viewHolder.xunChaBuildAdapter.addDatas(data.getBuildingList());
        }else {
            viewHolder.rlv_build_list.setVisibility(View.GONE);
        }
        viewHolder.tv_date.setText(data.getCheckStartTimeStr() + "-" + data.getCheckEndTimeStr());
    }

}
